﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SimpleData.Infrastructure.Provider;
using Moq;
using SimpleData.ObjectModel;
using SimpleData.ObjectModel.Design;

namespace SimpleData
{
	internal static class ProviderExtensions
	{


		public static ProviderMockBuilder<TName, TService> Provider<TName, TService>( this IMockInit init )
		{
			return new ProviderMockBuilder<TName, TService>();
		}


		public static ProviderMockBuilder<TName, TService> Create<TName, TService>( this ProviderMockBuilder<TName, TService> mock, TName name, TService service )
		{
			mock.Data[ name ] = service;
			return mock;
		}

		public static ProviderMockBuilder<RuleName, IRuleBuilderFactory> Create( this ProviderMockBuilder<RuleName, IRuleBuilderFactory> mock, RuleName name, IRuleBuilder service )
		{
			Mock<IRuleBuilderFactory> factory = new Mock<IRuleBuilderFactory>();

			factory
				.Setup( f => f.CreateRuleBuilder( It.IsAny<Rule>() ) )
				.Returns( service );

			return mock.Create( name, factory.Object );
		}
	}


	internal class ProviderMockBuilder<TName, TService> : MockBuilder<IProvider<TName, TService>>
	{

		public ProviderMockBuilder()
			: base()
		{
			this.Data = new Dictionary<TName, TService>();

			this.Mock
				.Setup( provider => provider.Create( It.IsAny<TName>() ) )
				.Returns<TName>( name => this.Data[ name ] );
		}

		public Dictionary<TName, TService> Data
		{
			get;
			private set;
		}
	}
}
